<?php

class Module extends ErrorMessage
{
	function readConfigProfilo( $livello, &$moduli, &$script, &$dscModuli, &$profModuli )
	{
		return;
		
		$line = 'V~1';
		$liv = split('\|', $livello);
		foreach( $liv as $key => $val )
		{
			$line.= sprintf("|TAG%02d@livello=%s", $key, $val);
		}
		$resp = $this->socket->reqRemoteRows("LIVELLI_CONFIG_LOAD $line", $rows);
		
		foreach( $moduli as $key1 => $val1 )
		{
			$profModuli[ $key1 ] = '';
			
			foreach( $rows as $key => $val )
			{
				$ary1 = split("=",$val);
				$ary2 = split("\:",$ary1[1]);
				
				if ( $ary2[0] == $val1 && $ary2[1] == $script[$key1] && $ary1[0] == 'MODULO_NO' ) 
				{
					//$this->appLog->logWrite("MODULO_NO $ary2[0] == $val1 && $ary2[1] == ".$this->request['script'][$key1]);
					$profModuli[ $key1 ] = 'MODULO_NO';
					break;
				}
				if ( $ary2[0] == $val1 && $ary2[1] == $script[$key1] && $ary1[0] == 'MODULO_NOWRITE' ) 
				{
					//$this->appLog->logWrite("MODULO_NOWRITE $ary2[0] == $val1 && $ary2[1] == ".$this->request['script'][$key1]);
					$profModuli[ $key1 ] = 'MODULO_NOWRITE';
					break;
				}
			}
		}
	}

	function defModuli( &$mod, &$do, &$dsc, &$ass )
	{
		$mod[] = 'appuntamenti';
		$do[]  = 'appuntamenti';
		$dsc[] = 'Appuntamenti : Gestione Appuntamenti';
		$ass[] = '';
		
		$mod[] = 'appuntamenti';
		$do[]  = 'appuntamentiList';
		$dsc[] = 'Appuntamenti : Lista Appuntamenti';
		$ass[] = '';
		
		$mod[] = 'appuntamenti';
		$do[]  = 'appuntamentiRiepilogo';
		$dsc[] = 'Appuntamenti : Riepilogo Appuntamenti';
		$ass[] = '';

		$mod[] = 'clienti';
		$do[]  = 'clienti';
		$dsc[] = 'Clienti : Gestione Clienti';
		$ass[] = '';

		$mod[] = 'clienti';
		$do[]  = 'clientiDati';
		$dsc[] = 'Clienti : Dettaglio Clienti';
		$ass[] = '';

		$mod[] = 'clienti';
		$do[]  = 'comunicazioni';
		$dsc[] = 'Clienti : Comunica con i Clienti';
		$ass[] = '';
		
		$mod[] = 'clienti';
		$do[]  = 'gruppi';
		$dsc[] = 'Clienti : Gestione Gruppi';
		$ass[] = '';

		$mod[] = 'clienti';
		$do[]  = 'gruppiDati';
		$dsc[] = 'Clienti : Dettaglio Gruppi';
		$ass[] = '';

		$mod[] = 'campagne';
		$do[]  = 'reportCampagna';
		$dsc[] = 'Report : Campagne';
		$ass[] = '';
		
		$mod[] = 'campagne';
		$do[]  = 'reportMessaggi';
		$dsc[] = 'Report : Messaggi';
		$ass[] = '';
		
		$mod[] = 'grafici';
		$do[]  = 'grafici';
		$dsc[] = 'Report : Grafici';
		$ass[] = '';
		
		$mod[] = 'risorse';
		$do[]  = 'agende';
		$dsc[] = 'Risorse : Agende';
		$ass[] = '';

		$mod[] = 'risorse';
		$do[]  = 'agendeDati';
		$dsc[] = 'Risorse : Dettaglio Agende';
		$ass[] = '';

		$mod[] = 'risorse';
		$do[]  = 'risorse';
		$dsc[] = 'Risorse : Risorse';
		$ass[] = '';
		
		$mod[] = 'risorse';
		$do[]  = 'risorseDati';
		$dsc[] = 'Risorse : Dettaglio Risorse';
		$ass[] = '';

		$mod[] = 'risorse';
		$do[]  = 'rGruppi';
		$dsc[] = 'Risorse : Categorie risorse';
		$ass[] = '';
		
		$mod[] = 'risorse';
		$do[]  = 'rGruppiDati';
		$dsc[] = 'Risorse : Dettaglio Categorie';
		$ass[] = '';

		$mod[] = 'risorse';
		$do[]  = 'prestazioni';
		$dsc[] = 'Risorse : Prestazioni';
		$ass[] = '';
		
		$mod[] = 'risorse';
		$do[]  = 'prestazioniDati';
		$dsc[] = 'Risorse : Dettaglio Prestazioni';
		$ass[] = '';
		
		$mod[] = 'risorse';
		$do[]  = 'pGruppi';
		$dsc[] = 'Risorse : Tipologie Prestazioni';
		$ass[] = '';

		$mod[] = 'risorse';
		$do[]  = 'pGruppiDati';
		$dsc[] = 'Risorse : Dettaglio Tipologie';
		$ass[] = '';
		
		$mod[] = 'risorse';
		$do[]  = 'prestazioni';
		$dsc[] = 'Risorse : Prestazioni';
		$ass[] = '';
		
		$mod[] = 'risorse';
		$do[]  = 'classePrestazioni';
		$dsc[] = 'Risorse : Classe Prestazioni';
		$ass[] = '';

		$mod[] = 'risorse';
		$do[]  = 'classePrestazioniDati';
		$dsc[] = 'Risorse : Dettaglio Classe Prestazioni';
		$ass[] = '';

		$mod[] = 'marketing';
		$do[]  = 'marketing';
		$dsc[] = 'Marketing : Marketing';
		$ass[] = '';
		
		$mod[] = 'marketing';
		$do[]  = 'scadenzeMarketing';
		$dsc[] = 'Marketing : Scadenze';
		$ass[] = '';
		
		$mod[] = 'impostazioni';
		$do[]  = 'utenti';
		$dsc[] = 'Impostazioni : Utenti';
		$ass[] = '';
		
		$mod[] = 'impostazioni';
		$do[]  = 'comunicazione';
		$dsc[] = 'Impostazioni : Comunicazioni';
		$ass[] = '';
		
		$mod[] = 'impostazioni';
		$do[]  = 'profiloCentro';
		$dsc[] = 'Impostazioni : Profilo centro';
		$ass[] = '';
		
		$mod[] = 'impostazioni';
		$do[]  = 'eventiLoyalty';
		$dsc[] = 'Impostazioni : Fedelta';
		$ass[] = '';
		
		$mod[] = 'impostazioni';
		$do[]  = 'prenotazioni';
		$dsc[] = 'Impostazioni : Prenotazioni';
		$ass[] = '';
		
		$mod[] = 'impostazioni';
		$do[]  = 'sito';
		$dsc[] = 'Impostazioni : Sito web';
		$ass[] = '';
		
		$mod[] = 'impostazioni';
		$do[]  = 'opzioni';
		$dsc[] = 'Impostazioni : Opzioni';
		$ass[] = '';
	}

	function readLivelli()
	{
		$line = '';
		$resp = $this->socket->reqRemoteRows("LIVELLI_LIST $line", $rows);
		
		$html = '';
		$nl = "\n";
		if ( count( $rows ) )
		{
			$prevLiv = -1;
			$tLiv = array();
			$sLiv = array();
			
			$sLiv[0] = '';
			$sLiv[1] = '';
			$sLiv[2] = '';
			$sLiv[3] = '';
			$sLiv[4] = '';
			
			reset($rows);
			foreach( $rows as $key => $val )
			{
				$fld = array();
				$pFld = split('\|', $rows[ $key - 1 ]);
				$fld = split('\|', $val);
				$aFld = split('\|', $rows[ $key + 1 ]);
				
				$fPrev = count( $pFld );
				$fCurr = count( $fld );
				$fSucc = count( $aFld );
				$type = 0;
				
				$livNew = $fCurr - 1;
				
				$tipo = 0;

				if ( $fPrev < $fCurr  )
				{
					if ( $fCurr < $fSucc ) $tipo = 1;
					if ( $fCurr == $fSucc ) $tipo = 3;
					if ( $fCurr > $fSucc ) $tipo = 5;
				}
				elseif ( $fPrev == $fCurr )
				{
					if ( $fCurr < $fSucc ) $tipo = 2;
					if ( $fCurr == $fSucc ) $tipo = 3;
					if ( $fCurr > $fSucc ) $tipo = 3;
				}
				else
				{
					if ( $fCurr < $fSucc ) $tipo = 2;
					if ( $fCurr == $fSucc ) $tipo = 4;
					if ( $fCurr > $fSucc ) $tipo = 4;
				}
						

				$this->appLog->logWrite("$tipo - $fPrev $fCurr $fSucc : $val");
				
				if ( $key == 0 )
				{
					$this->appLog->logWrite("--------------------------------------------- 0");
					$html.= '<li>'.$nl;
					$html.= '<span id="'.$val.'" onClick="editLivelli(\'\')" class="context-menu context-menu-theme-xp context-menu-example-trigger menu-default">'.$fld[$livNew].'</span>'.$nl;
					$html.= '<ul>'.$nl;
				}
				elseif ( $tipo == 1 )
				{
					$this->appLog->logWrite("--------------------------------------------- 1 ");
					$html.= '<li>'.$nl;
					$html.= '<span id="'.$val.'" onClick="editLivelli(\''.$val.'\')" class="context-menu context-menu-theme-xp context-menu-example-trigger menu-default">'.$fld[$livNew].'</span>'.$nl;
					$html.= '<ul>'.$nl;					
				}
				elseif ( $tipo == 2 )
				{
					$this->appLog->logWrite("--------------------------------------------- 2 $fCurr $fPrev");
					$diff = abs($fPrev - $fCurr);
					while( $diff-- )
					{
						$html.= '</ul>'.$nl;
						$html.= '</li>'.$nl;
					}
					$html.= '<li>'.$nl;
					$html.= '<span id="'.$val.'" onClick="editLivelli(\''.$val.'\')" class="context-menu context-menu-theme-xp context-menu-example-trigger menu-default">'.$fld[$livNew].'</span>'.$nl;
					$html.= '<ul>'.$nl;					
				}
				elseif ( $tipo == 3 )
				{
					$this->appLog->logWrite("--------------------------------------------- 3");
					$html.= '<li>';
					$html.= '<span id="'.$val.'" onClick="editLivelli(\''.$val.'\')" class="context-menu context-menu-theme-xp context-menu-example-trigger menu-default">'.$fld[$livNew].'</span>'.$nl;
					$html.= '</li>';
				}
				elseif ( $tipo == 4 )
				{
					$this->appLog->logWrite("--------------------------------------------- 4 $fCurr $fPrev");
					$diff = abs($fPrev - $fCurr);
					while( $diff-- )
					{
						$html.= '</ul>'.$nl;
						$html.= '</li>'.$nl;
					}
					$html.= '<li>'.$nl;
					$html.= '<span id="'.$val.'" onClick="editLivelli(\''.$val.'\')" class="context-menu context-menu-theme-xp context-menu-example-trigger menu-default">'.$fld[$livNew].'</span>'.$nl;
					$html.= '</li>'.$nl;					
				}
				elseif ( $tipo == 5 )
				{
					$this->appLog->logWrite("--------------------------------------------- 5 ");
					$html.= '<li>'.$nl;
					$html.= '<span id="'.$val.'" onClick="editLivelli(\''.$val.'\')" class="context-menu context-menu-theme-xp context-menu-example-trigger menu-default">'.$fld[$livNew].'</span>'.$nl;
					$html.= '</li>'.$nl;					
				}
			}
		}
		$html.= '</ul>'.$nl;
		$html.= '</li>'.$nl;
		$this->smarty->assign("html",$html);
		//$this->appLog->logWrite($html);
	}

	function exportCsv2( &$rows, $nomeFile = 'file.csv', $fields = '' )
	{
		if ( count( $rows ) )
		{
			reset($rows);
			$this->formatHeader( 'csv', $nomeFile );
			foreach( $rows as $val )
			{
				if ( $val == 'END_ROWS' ) break;
				$tmp = $this->formatCsv( $val ); 
				//$this->appLog->logWrite( $tmp );
				echo "$tmp\n";
			}
		}
	}

	function exportCsv( &$rows, $nomeFile = 'file.csv', $fields = '' )
	{
		if ( count( $rows ) )
		{
			reset($rows);
			$this->formatHeader( 'csv', $nomeFile );
			foreach( $rows as $val )
			{
				if ( $val == 'END_ROWS' ) break;
				$tmp = $this->formatCsv( $val ); 
				//$this->appLog->logWrite( $tmp );
				echo "$tmp\n";
			}
		}
	}
	
	function formatCsv( $val, $sep1 = '|', $sep2 = ';' )
	{
		$tmp1 = str_replace( ';', ',', $val );
		$tmp2 = str_replace( $sep1, $sep2, $tmp1 );
		$sost= array("\r\n", "\n", "\r","\f", "\\n","\\f");
		$tmp = str_replace( $sost, " ", $tmp2);
		return $tmp;
	}
	
	function formatHeader( $type = '', $nome = '', $size = '' )
	{
		if ( strpos($type, "lenght=") )
		{
			$pos = strpos($resp, "lenght=") + 7;
			$size = substr($resp, $pos);
			//$this->appLog->logWrite("************************ $size");
		}
		
		ob_clean();

		header("Cache-Control: public");
		header("Content-Description: File Transfer");
		header("Content-Transfer-Encoding: binary");
		if ( $size != '' ) header('Content-Length: '.$size);
		
		if ( preg_match( '/csv/i' , $type ) ) 
		{
			header( 'Content-type: text/csv' );
		}
		elseif ( preg_match( '/PDF/i' , $type ) ) 
		{
			header('Content-type: application/pdf'); 
		}
		elseif ( preg_match( '/OpenDocument Text/i' , $type ) ) 
		{
			header('Content-type: application/vnd.oasis.opendocument.text');
		}
		elseif ( preg_match( '/Microsoft Office Document/i' , $type ) ) 
		{
			if ( preg_match( '/xls/i' , $nome ) )
				header('Content-type: application/vnd.ms-excel');
			else
				header('Content-type: application/msword');
		}
		elseif ( preg_match( '/JPEG/i' , $type ) ) 
		{
			header('Content-type: image/jpeg');
		}
		elseif ( preg_match( '/PNG/i' , $type ) ) 
		{
			header('Content-type: image/png');
		}
		elseif ( preg_match( '/ZIP/i' , $type ) ) 
		{
			if ( preg_match( '/docx/i' , $nome ) ) 
				header('Content-type: application/vnd.openxmlformats-officedocument.wordprocessingml.document');
		}
		
		if ( $nome != '' )
			header( 'Content-Disposition: attachment; filename='.$nome );
	}
	
	function findLabel( $lbl = '' )
	{
		global $aryLang;
		foreach($aryLang as $key => $val)
		{
			if ( preg_match("/^#/", $val) )
			{
				continue;
			}
			elseif ( preg_match("/^$lbl/", $val) )
			{
				$tmp = split("\|", $aryLang[ $key ]);
				$lbl = $tmp[1];
				break;
			}
		}
		return $lbl;
	}

	function makeLine( &$lin, $fld, &$ary, $type = '' )
	{
		$chkLin = false;
		$tmp = preg_split("/\|/", $fld);
		foreach( $tmp as $val )
		{
			if ( $chkLin || $lin != '' ) $lin.= '|';
			if ( isset( $ary[$val] ))
			{
				if ( preg_match( "/^checked$/i", $ary[$val] ) )
				{
					$lin.= 'S';
				}
				elseif ( preg_match( "/^text$/i", $type ) )
				{
					$lin.= preg_replace("/\n/", "\f", $ary[$val]);
				}
				else
				{
					$lin.= $ary[$val];
				}
			}
			$chkLin = true;
		}
		if ( $chkLin && $lin == '' ) $lin.= '|';
	}

	function makeLine2( &$lin, $fld, &$ary, $type = '' )
	{
		if ( $lin == '' ) $lin = 'V~1';
		$chkLin = false;
		$tmp = preg_split("/\|/", $fld);
		foreach( $tmp as $val )
		{
			if ( $chkLin || $lin != '' ) $lin.= '|';
			if ( isset( $ary[$val] ))
			{
				if ( preg_match( "/^checked$/i", $ary[$val] ) )
				{
					$lin.= $val.'=S';
				}
				elseif ( preg_match( "/^text$/i", $type ) )
				{
					$lin.= $val.'='.preg_replace("/\n/", "\f", $ary[$val]);
				}
				else
				{
					$lin.= $val.'='.$ary[$val];
				}
			}
			else
			{
				$lin.= $val.'=';
			}
			$chkLin = true;
		}
		if ( $chkLin && $lin == '' ) $lin.= '|';
	}

	function makeTag( &$lin, $fld, &$ary, $type = '', $empty = 0 )
	{
		if ( $lin == '' ) $lin = 'V~1';
		
		$rep = array("\n");
		
		$pTag = 0;
		$chk = preg_split("/\|/", $lin);
		
		foreach( $chk as $val )
		{
			if ( preg_match( "/^TAG/", $val ) )
			{
				$pos = strpos( $val, "@" );
				$tTag = intval( substr( $val, 3, $pos ) );
				if ( $pTag < $tTag ) $pTag = $tTag;
			}
		}
		
		$pTag++;
		$chkLin = false;
		$tmp = preg_split("/\|/", $fld);
		foreach( $tmp as $val )
		{
			//if ( $chkLin || $lin != '' ) $lin.= '|';
			if ( isset( $ary[$val] ))
			{
				if ( $ary[$val] != '' || $empty )
				{
					if ( preg_match( "/^checked$/i", $ary[$val] ) )
					{
						$lin.= sprintf("|TAG%02d@%s=S",$pTag,$val);
						//$lin.= 'TAG'.sprintf("%02d",$pTag).'@'.$val.'=S';
						$this->appLog->logWrite("___________________________ OK1");
					}
					elseif ( $type == 'text' )
					{
						$ary[$val] = str_replace($rep, "\X13", $ary[$val]);
						$lin.= sprintf("|TAG%02d@%s=%s",$pTag,$val,$ary[$val]);
						$this->appLog->logWrite("___________________________ OK2");
						//$lin.= 'TAG'..'@'.$val.'='.preg_replace("/\n/", "\f", $ary[$val]);
					}
					else
					{
						$ary[$val] = str_replace($rep, "\X13", $ary[$val]);
						$lin.= sprintf("|TAG%02d@%s=%s",$pTag,$val,$ary[$val]);
						//$lin.= 'TAG'..'@'.$val.'='.$ary[$val];
						$this->appLog->logWrite("___________________________ OK3");
					}
					$pTag++;
				}
			}
			else
			{
				if ( $empty ) 
				{
					$lin.= sprintf("|TAG%02d@%s=",$pTag,$val);
					$pTag++;
				}
			}
			$chkLin = true;
		}
		//if ( $chkLin && $lin == '' ) $lin.= '|';
	}

	function convert( &$val, $type )
	{
		switch( $type )
		{
			case "text":
				$val = preg_replace("/\n/", "\f", $val);
				break;

			case "checked":
				$val = ( $val == 'checked' || $val == 'S' ) ? 'S': 'N' ;
				break;

			case "db2text":
				$val = preg_replace("/\f/", "\n", $val);
				break;
		}
	}

	function makeSingleAndSend( $cmd, $fld, &$ary )
	{
		if ( preg_match( "/^checked$/i", $ary[$fld] ) )
		{
			$val = 'S';
		}
		else
		{
			$val = $ary[$fld];
		}

		$resp = $this->socket->reqRemoteResponse( "$cmd $fld|$val" );
		if ( $this->checkResponse( $resp ) ) return false;
	}

	function clearValue( $fld, &$ary )
	{
		$tmp = preg_split("/\|/", $fld);
		foreach( $tmp as $val )
			if ( isset($ary[$val]) ) $ary[$val] = '';
	}

// $fld = chiavi da escludere
	function clearAryExcludeField( &$ary, $fld = '' )
	{
		if ( $fld != '' ) $fld.= '|';
		$fld.= 'sessid';
		$tmp = preg_split("/\|/", $fld);
		foreach( $this->request as $key => $val )
		{
			foreach( $tmp as $val2)
			{
				if ( ! array_key_exists( $val2, $ary ) )
				{
					print $key."=".$ary[$key]."<br>";
					if ( isset( $ary[$key] ) ) $ary[$key] = '';
				}
			}
		}
	}

	function fillValue( $fld, &$ary, $tipo = '' )
	{
		$tmp = preg_split("/\|/", $fld);
		foreach( $tmp as $val )
		{
			if ( is_array( $ary[$val] ) )
			{
				foreach( $ary[$val] as $key1 => $val1 )
				{
					$this->insertValue( $ary[$val][ $key1 ] , $val1, $tipo );
				}
				$this->smarty->assign($val, $ary[$val]);
			}
			else
			{
				if ( $tipo == 'checked' && ($ary[$val] == 'S' || $ary[$val] == 'on' || $ary[$val] == 'checked') )
					$this->smarty->assign($val, 'checked');
				elseif( $tipo == 'currency' )
					$this->smarty->assign($val, $this->numToCurrency( $ary[$val] ));
				else
					$this->smarty->assign($val, $ary[$val]);
			}
		}
	}

	function numToCurrency( $val )
	{
		return number_format($val, 2, ',', '.');
	}

	function currToNumber( $val )
	{
		$tmp = str_replace('.','',$val);
		return str_replace(',','.',$tmp);
	}

	function insertValue( &$var, $val, $tipo = '' )
	{
		if ( $tipo == 'checked' && ($ary[$val] == 'S' || $ary[$val] == 'on' || $ary[$val] == 'checked') )
			$var = 'checked';
		elseif( $tipo == 'currency' )
			$var = $this->numToCurrency( $val );
		else
			$var = $val;
	}

	// $fld = nome|desc|tipo|valore
	// nome = nome variabile
	// desc = descrizione
	// tipo = tipo
	// valore = valore
	function fillValueAssoc( $fld, &$ary )
	{
		$tmpAryName = array();
		$tmpAryValue = array();
		$tmpAryDesc = array();
		$tmpAryType = array();
		$tmp = preg_split("/\|/", $fld);
		foreach( $ary[ $tmp[0] ] as $key => $val )
		{
			$tmpAryName[ $key ] = $ary[ $tmp[0] ][ $key ];

			if ( isset( $ary[ $tmp[1] ][ $key ] ) )
				$tmpAryDesc[ $key ] = $ary[ $tmp[1] ][ $key ];
			else
				$tmpAryDesc[ $key ] = '';

			$tmpAryType[ $key ] = $ary[ $tmp[2] ][ $key ];

			if ( $ary[ $tmp[2] ][ $key ] == 'checked' && $ary[ $tmp[3] ][ $key ] == 'S' )
			{
				$tmpAryValue[ $key ] = 'checked';
			}
			else if ( $ary[ $tmp[2] ][ $key ] == 'text' )
			{
				$tmpAryValue[ $key ] = preg_replace("/\\0/", "\n", $ary[ $tmp[3] ][ $key ]);
			}
			else
			{
				$tmpAryValue[ $key ] = $ary[ $tmp[3] ][ $key ];
			}
		}

		$this->smarty->assign( $tmp[0], $tmpAryName );
		$this->smarty->assign( $tmp[1], $tmpAryDesc );
		$this->smarty->assign( $tmp[2], $tmpAryType );
		$this->smarty->assign( $tmp[3], $tmpAryValue );
	}

	function fillAssoc( $fld, &$ary )
	{
		$tmp = preg_split("/\|/", $fld);
		foreach( $ary[ $tmp[0] ] as $key => $val )
		{
			//print $val.") ".$key.") ".$tmp[1].") ".$ary[ $tmp[1] ][$key]."<br>";
			$this->smarty->assign($val, $ary[ $tmp[1] ][$key] );
		}
		if ( isset($tmp[2]) )
		{
			foreach( $ary[ $tmp[0] ] as $key => $val )
			{
				$this->smarty->assign($tmp[2].'_'.$val, $ary[ $tmp[2] ][$key] );
			}
		}
	}

	function initVars( $fld, &$ary )
	{
		$tmp = preg_split("/\|/", $fld);
		foreach( $tmp as $val )
		{
			if ( ! isset( $ary[$val] ) ) $ary[ $val ] = '';
		}
	}

	function checkResponse( &$resp, &$stack = array(), &$field = array() )
	{
		$stack = array();
		$field = array();
		if ( preg_match("/^NOVALIDATE/", $resp) )
		{
			$stack = preg_split("/\n/", $resp);
			array_shift($stack);
			return true;
		}

		if ( preg_match("/^ERROR/", $resp) )
		{
			$tmp = split( ':', $resp);
			if ( $this->errDesc( $tmp[1] ) )
			{
				$stack[] = $this->errDesc( $tmp[1] );
				if ( isset($tmp[2]) ) $field[] = $tmp[2];
			}
			return true;
		}
		return false;
	}

	function checkError( &$resp )
	{
		if ( gettype( $resp ) == 'array' )
		{
			if ( preg_match("/^ERROR/", $resp[0]) ) return true;
		}
		elseif ( preg_match("/^ERROR/", $resp) )
		{
			return true;
		}
		return false;
	}

	function makeKey( $num )
	{
		$chars = array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','x','y','w','z','0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','X','Y','W','Z');
		$numChars = count( $chars );
		$keyValue = '';
		while( $num-- )
		{
			$keyValue.= $chars[ intval( rand(0, $numChars) ) ];
		}
		return $keyValue;
	}
}

?>

